<?php
/* 
 * Project: oCoder Framework
 * File: /classes/helper.php
 * Purpose: class which build a common function
 * Author: Trung Hoang
 */

class Helper{

	//get current page url
	function currentPageURL() {
	    $curpageURL = 'http';
	    if ($_SERVER["HTTPS"] == "on") {
	    	$curpageURL.= "s";
	    }
	    $curpageURL.= "://";
	    if ($_SERVER["SERVER_PORT"] != "80") {
	    	$curpageURL.= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	    } else {
	    	$curpageURL.= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	    }
	    return $curpageURL;
    }

    //check login
    static function isLogin(){
    	if(isset($_SESSION['userLogin'])){
    		return true;
    	}else{
    		return false;
    	}
    }

    //get username login
    static function getUsernameLogin(){
    	if (isset($_SESSION['userLogin'])) {
    		return $_SESSION['userLogin']->Id;
    	}else{
    		return null;
    	}
    }
    // Set alias from title

     static function ToAlias($string) {
            $str = strtolower($string);
            $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
            $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
            $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
            $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
            $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
            $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
            $str = preg_replace("/(đ)/", 'd', $str);
            $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
            $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
            $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
            $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
            $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
            $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
            $str = preg_replace("/(Đ)/", 'D', $str);
            $str = str_replace(" ", "-", str_replace("&amp;*#39;","",$str));
            return $str;
        }
    public static function Authenticate($key){
        $idUser=self::getUsernameLogin();
        //include_once 'basemodel.php';
        $new =new BaseModel();
        $sql="SELECT
        nd_nguoidung.Id,
        nd_nhomchucnang.IdChucNang,
        nd_chucnang.TenChucNang,
        nd_chucnang.MoTa
        FROM
        nd_nguoidung
        INNER JOIN nd_nhomnguoidung ON nd_nhomnguoidung.IdNguoiDung = nd_nguoidung.Id
        INNER JOIN nd_nhom ON nd_nhomnguoidung.IdNhom = nd_nhom.Id
        INNER JOIN nd_nhomchucnang ON nd_nhomchucnang.IdNhom = nd_nhom.Id
        INNER JOIN nd_chucnang ON nd_nhomchucnang.IdChucNang = nd_chucnang.Id
        WHERE nd_nguoidung.id = {$idUser} 
        GROUP BY TenChucNang";
        $new->query($sql);
        $result= $new->get();
        $array=array();
        foreach ($result as $value) {
            $array[] = $value->TenChucNang;
        }
        return in_array($key, $array);
    }
    public static function redirect(){
        $alert = array(
                    'class' => 'alert-warning',
                    'message' => 'Bạn không có chức năng này!'
                    );
                $_SESSION['message'] = $alert;
                header( 'Location: '. BASE_URL . 'home/admin');
    }

}